package bro2Advanced.threads;

import java.util.concurrent.*;

public class ThreadExecute {

    /**
     * executorService使用有返回值的future
     *
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void executeWithCallable() throws ExecutionException, InterruptedException {
        // 创建一个包含5个线程的线程池
        ExecutorService executorService = Executors.newScheduledThreadPool(10);

        // 提交任务到ExecutorService执行，并获取Future对象
        Callable<String> task = new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "hello from " + Thread.currentThread().getName();
            }
        };

        // 通过Future对象获取任务的结果
        Future[] futures = new Future[10];
        for (int i = 0; i < 10; i++) {
            futures[i] = executorService.submit(task);
        }
        for (int i = 0; i < 10; i++) {
            System.out.println(futures[i].get());
        }
        // 关闭ExecutorService，不再接受新的任务，等待所有已提交的任务完成
        executorService.shutdown();
    }

    /**
     * executorService使用没有返回值的runnable
     */
    public static void executeWithRunnable() {
        // 创建一个包含5个线程的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        // 创建一个Runnable任务
        Runnable task = new Runnable() {
            public void run() {
                System.out.println("Hello from " + Thread.currentThread().getName());
            }
        };

        // 提交任务到ExecutorService执行
        for (int i = 0; i < 10; i++) {
            executorService.submit(task);
        }

        // 关闭ExecutorService，不再接受新的任务，等待所有已提交的任务完成
        executorService.shutdown();
    }

    /**
     * executorService 使用futureTask 获取返回值
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void executeWithFutureTask() throws ExecutionException, InterruptedException {
        // 创建一个固定大小的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        // 创建一系列callable
        Callable<Integer>[] tasks = new Callable[5];
        for (int i = 0; i < tasks.length; i++) {
            int index = i;
            tasks[i] = new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    TimeUnit.SECONDS.sleep(index + 1);
                    return (index + 1) * 100;
                }
            };

        }
        // 将 Callable 包装为 FutureTask，并提交到线程池
        FutureTask<Integer>[] futureTasks = new FutureTask[tasks.length];
        for (int i = 0; i < tasks.length; i++) {
            futureTasks[i] = new FutureTask<>(tasks[i]);
            executorService.submit(futureTasks[i]);
        }
        // 获取任务结果
        for (int i = 0; i < futureTasks.length; i++) {
            System.out.println("Result of task" + (i + 1) + ": " + futureTasks[i].get());
        }
        // 关闭线程池
        executorService.shutdown();
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        executeWithCallable();
        executeWithFutureTask();
    }
}
